local Theorem = require 'packages/tianshu/record/baseme'

--将技能添加进池子
Theorem.insertSkill=function(skill, skills,filterfuc)
  local skill_name = skill.name
  if filterfuc(skill) then --过滤
    table.insert(skills, skill_name)
  end
end

--从技能池中选择技能
---@param filterfuc @过滤函数，自定义什么函数不能过滤
---@param n @传参上限,即找多少个技能，太多有点浪费时间。下限为10
---@param m @选择技能数
---@param x @获得技能数
Theorem.getskillfrompool=function(player,room,filterfuc,n,m,x)
      local skill_pool, general_pool = {}, {}--技能池子
      local total=math.max(10,n)--不要遍历太多次
      local i = 0
      local black = {"emotest","tshugod__jiaxu1","tshugod__jiaxu2","tshujl__miaolvlingqi","tshujl__nianlvbu","tshujl__sajia","tshuox__xiaosha","tshuox__xiaojiu","tshuox__xiaotao","tshuox__xiaole","tshuox__xiaoshan","mouxusheng","tshugod__kuafu","tshugod__shaohao","tshugod__xuannv","tshugod__hanba","tshugod__caoe","tshugod__taoshen","tshujl__zhugeguo","tshujl__lingjv","tshujl__zhouyu","tshujl__caoying","tshujl__guansuo","tshujl__nianshou","tshujl__zhaoxiang","tshujl__xiaosha","tshujl__caochun","tshugod__hebo","tshugod__yuershen","tshulz__zuogu","tshulz__yougu","tshugod__yanluowang","tshujl__guanyu","tshujl__zhaoyun","tshuss__qinglong","tshuss__baihu","tshuss__zhuque","tshuss__xuanwu","tshuxx__jiaomujiao","tshuxx__kangjinlong","tshuxx__yihuoshe","tshuxx__xingrima","tshuxx__kuimulang","tshuxx__canshuiyuan","tshuxx__doumuxie","tshuxx__weiyueyan","tshuxx__xingxiang","tshujl__zhouyi","lingju","nos__zhuran","tshujl__nianzhugeliang","jbsao__wuyaowang","ty__tengfanglan","jbs_sairen","luyi","dingfuren","zuoci","os__zhaoxiang","zhaoxiang","ol_ex__zuoci","buqun__ailing"}
      local gen = {}
      for _, g in ipairs(black) do
          table.insertIfNeed(gen,Fk.generals[g])
      end
      --此处添加技能
      local all_generals = Fk:getAllGenerals(gen) 
      for _, g in ipairs(all_generals) do
        if g.package.extensionName == "tshu" or g.name == "mouxusheng" then
          table.removeOne(all_generals,g)
        end
      end
      --嵌入技能-- replace Engine:getGeneralsRandomly
      for _ = 1, 999 do
        local general = table.random(all_generals)
        local skills = {}
        table.forEach(general.skills, function(s) Theorem.insertSkill(s, skills,filterfuc) end)
        local skill = table.random(skills)
        if skill and not table.contains(skill_pool, skill) then
          i = i + 1
          table.insert(skill_pool, skill)
          table.insert(general_pool, general.name)

          if i == total then break end
        end
      end
      --获取table前n项
      local function getElements(tbl,n)
        local result = {}
        for i = 1, n do
            table.insert(result, tbl[i])
        end
        return result
      end
      if #skill_pool==0 then return false end
      local pskills=getElements(skill_pool,math.min(m,#skill_pool))
      local generals=getElements(general_pool,math.min(m,#general_pool))
      player.default_reply = table.random(pskills, math.min(x,#skill_pool))
      -- 添加取消按钮
      room:askForCustomDialog(player, nil,
      "packages/utility/qml/ChooseSkillBox.qml", {
        pskills, 0, x, "#skillpo-choose:::" .. tostring(x),generals
      })
    local choice = player.reply_ready and json.decode(player.client_reply) or player.default_reply
    if choice and #choice > 0 then
    room:handleAddLoseSkills(player, table.concat(choice, "|"), nil, true,true)
    end
    return choice
end

--从技能池中选择技能
---@param filterfuc @过滤函数，自定义什么函数不能过滤
---@param n @传参上限,即找多少个技能，太多有点浪费时间。下限为10
---@param m @选择技能数
---@param x @获得技能数
Theorem.getskillfrompool2=function(player,room,filterfuc,n,m,x)
  local skill_pool, general_pool = {}, {}--技能池子
  local total=math.max(10,n)--不要遍历太多次
  local i = 0
  local black = {"emotest","tshugod__jiaxu1","tshugod__jiaxu2","tshujl__miaolvlingqi","tshujl__nianlvbu","tshujl__sajia","tshuox__xiaosha","tshuox__xiaojiu","tshuox__xiaotao","tshuox__xiaole","tshuox__xiaoshan","mouxusheng","tshugod__kuafu","tshugod__shaohao","tshugod__xuannv","tshugod__hanba","tshugod__caoe","tshugod__taoshen","tshujl__zhugeguo","tshujl__lingjv","tshujl__zhouyu","tshujl__caoying","tshujl__guansuo","tshujl__nianshou","tshujl__zhaoxiang","tshujl__xiaosha","tshujl__caochun","tshugod__hebo","tshugod__yuershen","tshulz__zuogu","tshulz__yougu","tshugod__yanluowang","tshujl__guanyu","tshujl__zhaoyun","tshuss__qinglong","tshuss__baihu","tshuss__zhuque","tshuss__xuanwu","tshuxx__jiaomujiao","tshuxx__kangjinlong","tshuxx__yihuoshe","tshuxx__xingrima","tshuxx__kuimulang","tshuxx__canshuiyuan","tshuxx__doumuxie","tshuxx__weiyueyan","tshuxx__xingxiang","tshujl__zhouyi","lingju","tshujl__nianzhugeliang","jbsao__wuyaowang","ty__tengfanglan","jbs_sairen","luyi","dingfuren","zuoci","os__zhaoxiang","zhaoxiang","ol_ex__zuoci","buqun__ailing"}
  local gen = {}
  for _, g in ipairs(black) do
      table.insertIfNeed(gen,Fk.generals[g])
  end
  --此处添加技能
  local all_generals = Fk:getAllGenerals(gen) 
  for _, g in ipairs(all_generals) do
    if g.package.extensionName == "tshu" or g.name == "mouxusheng" then
      table.removeOne(all_generals,g)
    end
  end
  --嵌入技能-- replace Engine:getGeneralsRandomly
  for _ = 1, 999 do
    local general = table.random(all_generals)
    local skills = {}
    table.forEach(general.skills, function(s) Theorem.insertSkill(s, skills,filterfuc) end)
    local skill = table.random(skills)
    if skill and not table.contains(skill_pool, skill) then
      i = i + 1
      table.insert(skill_pool, skill)
      table.insert(general_pool, general.name)

      if i == total then break end
    end
  end
  --获取table前n项
  local function getElements(tbl,n)
    local result = {}
    for i = 1, n do
        table.insert(result, tbl[i])
    end
    return result
  end
  if #skill_pool==0 then return false end
  local pskills=getElements(skill_pool,math.min(m,#skill_pool))
  local generals=getElements(general_pool,math.min(m,#general_pool))
  player.default_reply = table.random(pskills, math.min(x,#skill_pool))
  -- 添加取消按钮
  room:askForCustomDialog(player, nil,
  "packages/utility/qml/ChooseSkillBox.qml", {
    pskills, 0, x, "#skillpo2-choose:::" .. tostring(x),generals
  })
local choice = player.reply_ready and json.decode(player.client_reply) or player.default_reply
if choice and #choice > 0 then
room:handleAddLoseSkills(player, table.concat(choice, "|"), nil, true,true)
end
return choice
end

--从技能池中选择技能
---@param filterfuc @过滤函数，自定义什么函数不能过滤
---@param n @传参上限,即找多少个技能，太多有点浪费时间。下限为10
---@param m @选择技能数
---@param x @获得技能数
Theorem.getskillfrompool3=function(player,room,filterfuc,n,m,x)
  local skill_pool, general_pool = {}, {}--技能池子
  local total=math.max(10,n)--不要遍历太多次
  local i = 0
  local black = {"emotest","tshugod__jiaxu1","tshugod__jiaxu2","tshujl__miaolvlingqi","tshujl__nianlvbu","tshujl__sajia","tshuox__xiaosha","tshuox__xiaojiu","tshuox__xiaotao","tshuox__xiaole","tshuox__xiaoshan","mouxusheng","tshugod__kuafu","tshugod__shaohao","tshugod__xuannv","tshugod__hanba","tshugod__caoe","tshugod__taoshen","tshujl__zhugeguo","tshujl__lingjv","tshujl__zhouyu","tshujl__caoying","tshujl__guansuo","tshujl__nianshou","tshujl__zhaoxiang","tshujl__xiaosha","tshujl__caochun","tshugod__hebo","tshugod__yuershen","tshulz__zuogu","tshulz__yougu","tshugod__yanluowang","tshujl__guanyu","tshujl__zhaoyun","tshuss__qinglong","tshuss__baihu","tshuss__zhuque","tshuss__xuanwu","tshuxx__jiaomujiao","tshuxx__kangjinlong","tshuxx__yihuoshe","tshuxx__xingrima","tshuxx__kuimulang","tshuxx__canshuiyuan","tshuxx__doumuxie","tshuxx__weiyueyan","tshuxx__xingxiang","tshujl__zhouyi","lingju","tshujl__nianzhugeliang","jbsao__wuyaowang","ty__tengfanglan","jbs_sairen","luyi","dingfuren","zuoci","os__zhaoxiang","zhaoxiang","ol_ex__zuoci","buqun__ailing"}
  local gen = {}
  for _, g in ipairs(black) do
      table.insertIfNeed(gen,Fk.generals[g])
  end
  --此处添加技能
  local all_generals = Fk:getAllGenerals(gen) 
  for _, g in ipairs(all_generals) do
    if g.package.extensionName == "tshu" or g.name == "mouxusheng" then
      table.removeOne(all_generals,g)
    end
  end
  --嵌入技能-- replace Engine:getGeneralsRandomly
  for _ = 1, 999 do
    local general = table.random(all_generals)
    local skills = {}
    table.forEach(general.skills, function(s) Theorem.insertSkill(s, skills,filterfuc) end)
    local skill = table.random(skills)
    if skill and not table.contains(skill_pool, skill) then
      i = i + 1
      table.insert(skill_pool, skill)
      table.insert(general_pool, general.name)

      if i == total then break end
    end
  end
  --获取table前n项
  local function getElements(tbl,n)
    local result = {}
    for i = 1, n do
        table.insert(result, tbl[i])
    end
    return result
  end
  if #skill_pool==0 then return false end
  local pskills=getElements(skill_pool,math.min(m,#skill_pool))
  local generals=getElements(general_pool,math.min(m,#general_pool))
  player.default_reply = table.random(pskills, math.min(x,#skill_pool))
  -- 添加取消按钮
  room:askForCustomDialog(player, nil,
  "packages/utility/qml/ChooseSkillBox.qml", {
    pskills, 0, x, "#skillpo3-choose:::" .. tostring(x),generals
  })
local choice = player.reply_ready and json.decode(player.client_reply) or player.default_reply
return choice
end

--选择自己的技能
Theorem.chooseplayerskill=function(player,room)
  local skills={}
  for _, s in ipairs(player.player_skills) do
    if not (s.attached_equip or s.name[#s.name] == "&") and not string.find(s.name, "#") then
      table.insertIfNeed(skills, s.name)
    end
  end
  
  room:askForCustomDialog(player, nil,
  "packages/utility/qml/ChooseSkillBox.qml", {
    skills, 0, #skills, "#playerskillpo-choose:::" .. tostring(x)
  })
  if  player.reply_ready then
    local choice = json.decode(player.client_reply) 
    return choice
  end


end
Theorem.choosemarkskill=function(player,skills,room,x)
  room:askForCustomDialog(player, nil,
  "packages/utility/qml/ChooseSkillBox.qml", {
    skills, x,  x, "#markskillpo-choose:::" .. tostring(x)
  })
  if  player.reply_ready then
    local choice = json.decode(player.client_reply) 
   
    return choice
  end
end

Theorem.choosemark2skill=function(player,skills,room,x)
  room:askForCustomDialog(player, nil,
  "packages/utility/qml/ChooseSkillBox.qml", {
    skills, x,  x, "#markskillpo3-choose:::" .. tostring(x)
  })
  if  player.reply_ready then
    local choice = json.decode(player.client_reply) 
   
    return choice
  end
end

Theorem.choosemarkskillask=function(player,skills,room,min,x,ask)
  room:askForCustomDialog(player, nil,
  "packages/utility/qml/ChooseSkillBox.qml", {
    skills, min, x, ask..":::" .. tostring(x)
  })
  if  player.reply_ready then
    local choice = json.decode(player.client_reply) 
   
    return choice
  end
end

Fk:loadTranslationTable{
  ["#skillpo-choose"]="天书：选择获得0-%arg个技能（可取消）",

  ["#skillpo2-choose"]="选择获得0-%arg个临时技能（可取消）",

  ["#skillpo3-choose"]="选择获得0-%arg个技能加入公共技能库（可取消）",

  ["#playerskillpo-choose"]="选择自己的若干技能",
  
  ["#markskillpo-choose"] = "天书：选择%arg个技能移除之",

  ["#markskillpo3-choose"] = "天书：选择%arg个技能于公共技能库进行交换",

}
  return Theorem